from concurrent import futures
from run_pytest_parallel import run_command
import time
import argparse

commands = [
    "python3 tools/run_pysdk_remote_infinity_test.py --pytest_mark='not complex'",
    "python3 tools/run_parallel_test.py --pytest_mark='not complex'",
    "python3 tools/run_http_api.py --pytest_mark='not complex'",
    "python3 tools/sqllogictest.py"
]

LOG_PATH = "/var/infinity/log/infinity.log"
# TEST_SEC = 3600 # 1 hour
TEST_SEC = 10  # run once

if __name__ == "__main__":
    parser = argparse.ArgumentParser(description="Pytest Parallel Continuous")
    parser.add_argument("-t", "--test_sec", type=int, default=TEST_SEC, dest="test_sec")
    parser.add_argument("-l", "--log_path", type=str, default=LOG_PATH, dest="log_path")
    args = parser.parse_args()
    test_sec = args.test_sec
    infinity_log_path = args.log_path

    command_failed = False
    begin_time = time.time()
    try:
        with futures.ProcessPoolExecutor(max_workers=1) as executor:
            while not command_failed and time.time() - begin_time < test_sec:
                futs = [executor.submit(run_command, cmd) for cmd in commands]
                for fut in futures.as_completed(futs):
                    try:
                        command, elapsed_time, stdout, stderr = fut.result()
                        print(f"Command '{command}' executed in {elapsed_time:.2f} seconds")
                        print(f"Command '{command}' output:\n{stdout}")
                        if stderr:
                            print(f"Command '{command}' error:\n{stderr}")
                            command_failed = True
                            break
                    except RuntimeError as e:
                        print(e)
                        command_failed = True
                        break
                if command_failed:
                    break

    except Exception as e:
        print(e)
        command_failed = True
    if command_failed:
        exit(-1)
